package com.tyg.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tyg.entity.MenuInfo;
import com.tyg.mapper.MenuInfoMapper;
import com.tyg.service.MenuInfoService;
import com.tyg.util.ResultCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 菜单表 前端控制器
 * </p>
 *
 * @author zhangjingnan
 * @since 2022-10-22
 */
@RestController
@RequestMapping("/menu-info")
@CrossOrigin
@Slf4j
public class MenuInfoController {

    @Autowired
    MenuInfoMapper menuMapper;

    @Autowired
    MenuInfoService menuInfoService;

    @PostMapping("/getMenuList")
    public ResultCode getMenuList(int roleId) {
        System.out.println("用户角色"+roleId);
        Map<String,Object> map=new HashMap<>();
        map.put("roleId",roleId);
        map.put("parentId", 0);
//        List<MenuInfo> menuList =menuMapper.getMenuList(map);
        List<MenuInfo> menuList=menuInfoService.getMenuList(map);
        for(MenuInfo menu:menuList) {
            if(menu.getParentId()==0) {
                Map<String,Object> subMap =new HashMap<>();
                subMap.put("roleId",roleId);
                subMap.put("parentId",menu.getMenuId());
//                List<MenuInfo> sunMenuList = menuMapper.getMenuList(subMap);
                List<MenuInfo> sunMenuList=menuInfoService.getMenuList(subMap);
                menu.setSubMenuList(sunMenuList);
            }
        }

        return ResultCode.success(menuList);

    }


    @PostMapping("/getAllMenuList")
    public ResultCode getAllMenuList() {
        QueryWrapper<MenuInfo> queryWrapper=new QueryWrapper<MenuInfo>();
        queryWrapper.eq("parentId",0);//查询一级菜单
//        List<MenuInfo> menuList =menuMapper.selectList(queryWrapper);
        List<MenuInfo> menuList =menuInfoService.selectList(queryWrapper);
        for(MenuInfo menu:menuList) {
            if(menu.getParentId() == 0) {
                QueryWrapper<MenuInfo> subQueryWrapper=new QueryWrapper<MenuInfo>();
                subQueryWrapper.eq("parentId",menu.getMenuId());//查询二级菜单
//                List<MenuInfo> subMenuList =menuMapper.selectList(subQueryWrapper);
                List<MenuInfo> subMenuList =menuInfoService.selectList(subQueryWrapper);
                menu.setSubMenuList(subMenuList);
            }
        }
        return ResultCode.success(menuList);
    }

}

